/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source 
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.  
 */

package jdbcmanager.tree;

import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;

/**
 * Tree cell renderer.
 *
 * @author Francisco Morero Peyrona
 */
final class CellRenderer extends JPanel implements TreeCellRenderer
{
   private static Color textSelectColor      = UIManager.getColor( "Tree.selectionForeground" );
   private static Color textNonSelectColor   = UIManager.getColor( "Tree.textForeground" );
   private static Color backSelectColor      = UIManager.getColor( "Tree.selectionBackground" );
   private static Color backNonSelectColor   = UIManager.getColor( "Tree.textBackground" );
   private static Color borderSelectionColor = UIManager.getColor( "Tree.selectionBorderColor" );

   private JLabel icon;
   private JLabel name;
   
   //-------------------------------------------------------------------------//

   CellRenderer()
   {
      super( new FlowLayout( FlowLayout.LEADING, 3, 0 ) );

      setOpaque( false );
      
      this.icon = new JLabel();
      this.name = new JLabel();
      
      add( this.icon );
      add( this.name );
   }

   public Component getTreeCellRendererComponent( JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus )
   {
      JdbcTreeNode node = (JdbcTreeNode) value;

      this.icon.setIcon( node.getIcon() );

      this.name.setText( " "+ node.getName() +" " );
      this.name.setFont( tree.getFont() );
      this.name.setOpaque( selected );

      if( selected )
      {
         this.name.setForeground( textSelectColor );
         this.name.setBackground( backSelectColor );
         this.name.setBorder( BorderFactory.createLineBorder( borderSelectionColor, 1 ) );
      }
      else
      {
         this.name.setBorder( BorderFactory.createLineBorder( backNonSelectColor, 1 ) );
         this.name.setForeground( textNonSelectColor );
      }

      return this;
   }
}